<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left">
        <div class="left-content">
          <h1 class="welcome-text">白驹智能日程管理系统</h1>
          <p class="slogan">珍惜时间，提升效率</p>
          <div class="illustration-wrapper">
            <img
              src="@/assets/login-illustration.svg"
              alt="登录插图"
              class="login-illustration"
            />
          </div>
        </div>
      </div>

      <div class="login-form-container">
        <div class="form-wrapper">
          <h2 class="login-title">欢迎登录</h2>
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="用户名"
                prefix-icon="User"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="密码"
                prefix-icon="Lock"
                show-password
              />
            </el-form-item>

            <el-button
              :loading="loading"
              type="primary"
              class="login-button"
              @click="handleLogin"
            >
              确认登录
            </el-button>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { User, Lock } from "@element-plus/icons-vue";
import { useUserStore } from "@/stores/user";
import { login } from "@/api/auth";
const router = useRouter();
const loading = ref(false);
const userStore = useUserStore();
console.log("userStore:", userStore);
const loginForm = reactive({
  username: "",
  password: "",
});

const loginFormRef = ref(null);

const loginRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const handleLogin = async () => {
  if (!loginFormRef.value) return;

  try {
    await loginFormRef.value.validate(async (valid) => {
      if (valid) {
        loading.value = true;
        try {
          await userStore.login(loginForm);
          ElMessage.success("登录成功");
          await userStore.getUserInfo();
          console.log('获取用户成功');
          console.log("userStore:", userStore);
          router.push("/dashboard");
        } catch (error) {
          console.error("登录失败:", error);
          ElMessage.error(error.message || "登录失败，请检查用户名和密码");
        }
      }
    });
  } catch (error) {
    console.error("表单验证失败:", error);
    ElMessage.error("请填写完整的登录信息");
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  background: #4169e1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.login-content {
  display: flex;
  background: white;
  border-radius: 8px;
  width: 80%;
  max-width: 1000px;
  height: 600px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-left {
  flex: 1;
  background: #4169e1;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.left-content {
  text-align: center;
  width: 100%;
}

.welcome-text {
  font-size: 32px;
  margin-bottom: 16px;
  font-weight: 500;
}

.slogan {
  font-size: 16px;
  margin-bottom: 40px;
  opacity: 0.9;
}

.illustration-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-illustration {
  width: 80%;
  max-width: 320px;
}

.login-form-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.form-wrapper {
  width: 100%;
  max-width: 360px;
}

.login-title {
  text-align: center;
  font-size: 24px;
  color: #333;
  margin-bottom: 40px;
  font-weight: 500;
}

.login-form {
  :deep(.el-input__wrapper) {
    background-color: #f5f7fa;
  }

  :deep(.el-input__inner) {
    height: 42px;
  }
}

.login-button {
  width: 100%;
  margin-top: 20px;
  height: 42px;
  font-size: 16px;
}
</style>
